package com.example.news;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.TextureView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.news.dao.UserDao;
import com.example.news.entity.User;
import com.example.news.entity.Userinfo;

import java.util.Objects;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_user_name;
    private EditText et_user_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        et_user_name = findViewById(R.id.et_user_name);
        et_user_pwd = findViewById(R.id.et_user_pwd);
        findViewById(R.id.et_reg_new).setOnClickListener(this);
        findViewById(R.id.btn_login).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_login) {
            String userName = et_user_name.getText().toString();
            String userPwd = et_user_pwd.getText().toString();
            if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPwd)) {
                Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            long count = UserDao.queryLogin(userName, userPwd);
            User user = UserDao.queryLoginUser(userName, userPwd);
            if(Objects.nonNull(user)){
                Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
                Userinfo userinfo = new Userinfo();
                userinfo.setUserName(user.getUserName());
                userinfo.setNickName(user.getNickName());
                Userinfo.setUserinfo(userinfo);
                finish();
            }else {
                Toast.makeText(this, "登陆失败，请联系管理员", Toast.LENGTH_SHORT).show();
            }
        } else if (id == R.id.et_reg_new) {
            Intent intent = new Intent(this, RegisterNewUserActivity.class);
            startActivity(intent);
        }
    }
}